package Abilities

import (
	"PBMessage"
	//. "SGNet"
	. "GameLogic/Scene/Unit"
	"github.com/golang/protobuf/proto"
	xml "misc/dom4g"
)

type AbiliAttack struct {
	AbilityInfo
}

func CreateAbiliAttack(node *xml.Element) IAbility {
	return &AbiliAttack{}
}

func (this *AbiliAttack) OnEnter() {
	this.AbiliUnit.GetCmdQueue().AddEvent(CommandType_Attack, this.OnAttackCommandPush)
}

func (this *AbiliAttack) OnAttackCommandPush(ints []int32, floats []float32, strs []string) {
	if this.AbiliUnit.IsDie == true {
		return
	}
	for i := 0; i < len(this.AbiliUnit.Weapons); i++ {
		weapon := this.AbiliUnit.Weapons[i]
		if weapon.AttackCD == weapon.AttackSpeed {
			sl := this.AbiliUnit.Scene
			targetUnit := sl.GetUnit(strs[0])
			//Send TO Self
			abiliAttack := &PBMessage.SC_AbilityAttack{}
			abiliAttack.UnitID = proto.String(this.AbiliUnit.GetBaseUnit().UUID)
			abiliAttack.WeaponId = proto.Int32(int32(i))
			abiliAttack.TargetUnitID = proto.String(strs[0])
			player := sl.GamePlayers[this.AbiliUnit.GamePlayerId]
			player.Sess.SendPacket(PBMessage.PBID_SC_AbilityAttack, abiliAttack)
			weapon.AttackCD = 0
			//Send To Target
			targetPlayer := sl.GamePlayers[targetUnit.GetBaseUnit().GamePlayerId]
			targetPlayer.Sess.SendPacket(PBMessage.PBID_SC_AbilityAttack, abiliAttack)
			//ExecAttack
			weapon.ExecAttack(targetUnit)
		}
	}
}

func (this *AbiliAttack) GetAbilityInfo() *AbilityInfo {
	return &this.AbilityInfo
}

func (this *AbiliAttack) GetId() int {
	return 1
}

func (this *AbiliAttack) Update() {
	for i := 0; i < len(this.AbiliUnit.Weapons); i++ {
		weapon := this.AbiliUnit.Weapons[i]
		if weapon.AttackCD < weapon.AttackSpeed {
			weapon.AttackCD += 0.016
		} else if weapon.AttackCD > weapon.AttackSpeed {
			weapon.AttackCD = weapon.AttackSpeed
		}
	}
}
